Best Time to Buy and Sell Stock III
LeetCode 123 | Difficulty: Hardβ
HardProblem Descriptionβ
You are given an array prices where prices[i] is the price of a given stock on the i^th day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Constraints:
- `1 <= prices.length <= 10^5`
- `0 <= prices[i] <= 10^5`
Topics: Array, Dynamic Programming
Approachβ
Dynamic Programmingβ
Break the problem into overlapping subproblems. Define a state (what information do you need?), a recurrence (how does state[i] depend on smaller states?), and a base case. Consider both top-down (memoization) and bottom-up (tabulation) approaches.
When to use
Optimal substructure + overlapping subproblems (counting ways, min/max cost, feasibility).
Solutionsβ
Solution 1: C# (Best: 96 ms)β
| Metric | Value |
|---|---|
| Runtime | 96 ms |
| Memory | 23.4 MB |
| Date | 2019-02-24 |
Solution
public class Solution {
public int MaxProfit(int[] prices) {
if (prices == null || prices.Length == 0)
{
return 0;
}
int k=2, n=prices.Length;
int[,] profit = new int[k + 1, n+1];
// fill the table in bottom-up fashion
for (int i = 1; i <= k; i++) {
int prevDiff = Int32.MinValue;
for (int j = 1; j < n; j++) {
prevDiff = Math.Max(prevDiff, profit[i - 1,j - 1] - prices[j - 1]);
profit[i,j] = Math.Max(profit[i,j - 1], prices[j] + prevDiff);
}
}
return profit[k,n - 1];
}
}
π 2 more C# submission(s)
Submission (2019-02-24) β 100 ms, 23.6 MBβ
public class Solution {
public int MaxProfit(int[] prices) {
if (prices == null || prices.Length == 0)
{
return 0;
}
int n = prices.Length;
int profit = 0;
// scan from left
// left[i] keeps the max profit from 0 to i
int[] left = new int[n];
int min = prices[0];
for (int i = 1; i < n; i++)
{
left[i] = Math.Max(left[i - 1], prices[i] - min);
min = Math.Min(min, prices[i]);
}
// scan from right
// right[i] keeps the max profit from i to n - 1
int[] right = new int[n];
int max = prices[n - 1];
for (int i = n - 2; i >= 0; i--)
{
right[i] = Math.Max(right[i + 1], max - prices[i]);
max = Math.Max(max, prices[i]);
profit = Math.Max(profit, left[i] + right[i]);
}
return profit;
}
}
Submission (2019-02-24) β 104 ms, 23.6 MBβ
public class Solution {
public int MaxProfit(int[] prices) {
if (prices == null || prices.Length == 0)
{
return 0;
}
int k=2, n=prices.Length;
int[,] profit = new int[k + 1, n+1];
// For day 0, you can't earn money
// irrespective of how many times you trade
for (int i = 0; i <= k; i++)
profit[i,0] = 0;
// profit is 0 if we don't do any transation
// (i.e. k =0)
for (int j = 0; j <= n; j++)
profit[0,j] = 0;
// fill the table in bottom-up fashion
for (int i = 1; i <= k; i++) {
int prevDiff = Int32.MinValue;
for (int j = 1; j < n; j++) {
prevDiff = Math.Max(prevDiff, profit[i - 1,j - 1] - prices[j - 1]);
profit[i,j] = Math.Max(profit[i,j - 1], prices[j] + prevDiff);
}
}
return profit[k,n - 1];
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| DP (2D) | $O(n Γ m)$ | $O(n Γ m)$ |
Interview Tipsβ
Key Points
- Break the problem into smaller subproblems. Communicate your approach before coding.
- Define the DP state clearly. Ask: "What is the minimum information I need to make a decision at each step?"
- Consider if you can reduce space by only keeping the last row/few values.